import win32gui
import win32api
from win32gui import *
from win32api import *
from win32process import *
import win32con
classname = "ThunderRT6FormDC"
titlename = "试卷评析(当前总共有成绩考生：1人)"
#获取句柄
hwnd = win32gui.FindWindow(classname, titlename)
pwin1=win32gui.FindWindowEx(hwnd ,None,"RichTextWndClass",None)

left, top, right, bottom = win32gui.GetWindowRect(pwin1)
print(left, top, right, bottom)
print(pwin1)

# 获取窗口文本不含截尾空字符的长度
# 参数：窗口句柄； 消息类型； 参数WParam； 参数IParam
bufSize = win32api.SendMessage(pwin1, win32con.WM_GETTEXTLENGTH, 0, 0) +1
# 利用api生成Buffer
strBuf = win32gui.PyMakeBuffer(bufSize)
print('bufSize:',bufSize)
print('strBuf:',strBuf)
# 发送消息获取文本内容
# 参数：窗口句柄； 消息类型；文本大小； 存储位置
#length = win32gui.SendMessage(pwin1, win32con.WM_GETTEXT, bufSize, strBuf)

# 反向内容，转为字符串
text = str(strBuf[:-1])
print('text1: ', text)
address, length = win32gui.PyGetBufferAddressAndLen(strBuf) 
print('length: ', length)

#length = win32api.SendMessage(pwin1, win32con.WM_GETTEXTLENGTH)+1
#print('Length: ', length)

buf = '0'*length
print('get: ', win32api.SendMessage(pwin1, win32con.WM_GETTEXT, length, buf))
print('text: ', text)
